/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VariableDlg.java
 *
 * Created on Apr 19, 2010, 1:00:52 PM
 */

package buggymastercode;

import org.jdesktop.application.Action;

/**
 *
 * @author jalvarez
 */
public class VariableDialog extends javax.swing.JDialog {

    /** Creates new form VariableDlg */
    public VariableDialog(java.awt.Frame parent) {
        super(parent, true);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txJavaName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txVbName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        txDataType = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(buggymastercode.BuggyMasterCodeApp.class).getContext().getResourceMap(VariableDialog.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        txJavaName.setName("txJavaName"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        txVbName.setName("txVbName"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(buggymastercode.BuggyMasterCodeApp.class).getContext().getActionMap(VariableDialog.class, this);
        jButton1.setAction(actionMap.get("saveVariable")); // NOI18N
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        txDataType.setName("txDataType"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txVbName, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel3))
                            .addGap(34, 34, 34)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txDataType, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txJavaName, javax.swing.GroupLayout.PREFERRED_SIZE, 286, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txVbName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txJavaName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(9, 9, 9)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txDataType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(15, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JTextField txDataType;
    private javax.swing.JTextField txJavaName;
    private javax.swing.JTextField txVbName;
    // End of variables declaration//GEN-END:variables

    public int getId() {
        return m_id;
    }

    public boolean edit(int id, int cl_id) {
        m_cl_id = cl_id;
        if (id != Db.CS_NO_ID) {
            String sqlstmt = "select var_vbname, var_javaname, var_datatype from tvariable where var_id = " + ((Integer)id).toString();
            DBRecordSet rs = new DBRecordSet();
            if (!Db.db.openRs(sqlstmt, rs)) {return false;}

            if (rs.getRows().isEmpty()) {
                m_id = Db.CS_NO_ID;
                this.txVbName.setText("");
                this.txJavaName.setText("");
                this.txDataType.setText("");
            }
            else {
                m_id = id;
                this.txVbName.setText(rs.getRows().get(0).get("var_vbname").toString());
                this.txJavaName.setText(rs.getRows().get(0).get("var_javaname").toString());
                this.txDataType.setText(rs.getRows().get(0).get("var_datatype").toString());
            }
        }
        else {
            m_id = Db.CS_NO_ID;
            this.txVbName.setText("");
            this.txJavaName.setText("");
            this.txDataType.setText("");
        }
        return true;
    }

    @Action
    public void saveVariable() {
        if (validateVariable()) {

            VariableObject var = new VariableObject();
            var.setId(m_id);
            var.setClId(m_cl_id);
            var.setVbName(this.txVbName.getText());
            var.setJavaName(this.txJavaName.getText());
            var.setDataType(this.txDataType.getText());
            if (var.saveVariable()) {
                m_id = var.getId();
            }
        }
    }

    private boolean validateVariable() {

        if (this.txVbName.getText().length() == 0) {
            G.showInfo("The vb name field is required");
            return false;
        }
        if (this.txJavaName.getText().length() == 0) {
            G.showInfo("The java name field is required");
            return false;
        }
        if (this.txDataType.getText().length() == 0) {
            G.showInfo("The data type field is required");
            return false;
        }
        return true;
    }

    private int m_id;
    private int m_cl_id;
}
